#include "junix.h"
#include <sys/msg.h>

struct msgbuf
{
	long mtype;
	char mtext[20];
};

int main(int argc, char **argv)
{
	if(argc==1)
	{
		fprintf( stderr, "Usage:%s file\n", argv[0] );
		exit(EXIT_FAILURE);
	}

	key_t key = ftok(argv[1], 0);
	if(key == -1)
		err_sys("ftok");

	fprintf(stdout,"key:%#08x\n",key);

	int msgid;
	if( (msgid=msgget(key, 0644|IPC_CREAT )) == -1 )
		err_sys("msgget");

	fprintf(stdout,"msqid:%#08x\n",msgid);

	struct msgbuf buf;
	int len=sizeof(buf.mtext)/sizeof(char);
	int type=5;

	buf.mtype = type;
	strncpy(buf.mtext,"Hello,junix",20);
	if( msgsnd(msgid, &buf, len, 0) == -1)
		err_sys("msgsnd");

	memset(&buf,0,sizeof(buf) );
	if(msgrcv(msgid, &buf, len, type, 0) == -1)
		err_sys("msgrcv");

	printf("%s\n",buf.mtext);

	return 0;
}

